package controller.v2;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import listener.WebContextListener;
import model.CityVO;
import model.MemberVO;
import service.v2.CityService;
import service.v2.MemberItemService;
import service.v2.MemberLevelItemService;
import service.v2.MemberService;
import service.v2.PushService;
import service.v2.SmsService;
import util.IPUtil;
import util.MD5Util;
import util.NumberUtil;
import util.PackageUtil;
import util.PayCommonUtil;
import util.PropertieUtil;
import util.PushUtil;
import util.SmsSendUtil;
import util.StringUtil;

@RequestMapping("/v2/login")
@Controller
public class LoginController {
	
	@Autowired
	private MemberLevelItemService memberLevelItemService;
	@Autowired
	private MemberService memberService;
	@Autowired
	private CityService cityService;
	@Autowired
	private SmsService smsService;
	@Autowired
	private PushService pushService;
	
	/**
	 * 手机号前缀查询
	 */
	@ResponseBody
	@RequestMapping(value="/telPrefix",method=RequestMethod.GET)
	public Object telPrefix(@RequestHeader (value="LANG", required=false) String lang,HttpServletRequest request, HttpServletResponse response){
		List<CityVO> cityList = cityService.getTelCountryList();
		
		List l =    cityService.packageCountry(cityList, lang);
		return PackageUtil.packageParams(200,"success",l,""); 
	}
	
	/**
	 * 手机号前缀查询
	 */
	/*@ResponseBody
	@RequestMapping(value="/telPrefixAll",method=RequestMethod.GET)
	public Object telPrefixAll(HttpServletRequest request, HttpServletResponse response){
		List<CityVO> cityList = cityService.getTelCountryList();
		
		List l =    cityService.packageCountryString(cityList);
		return PackageUtil.packageParams(200,"success",l,""); 
	}*/
	/**
	 * 密码登录
	 * @param memberVO 
	 * country 国家code
	 * country_id 国家id
	 * tel 手机号码
	 * pwd 密码
	 * @return 
	 * cuid 会员id
	 * token 令牌
	 */
	@ResponseBody
	@RequestMapping(value="/loginSub",method=RequestMethod.GET)
	public Object loginSub(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="OS", required=false) String os
			,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO){
		//验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		
		//验证手机号码
		String tel = memberVO.getTel();
		if(tel == null || "".equals(tel.trim())){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
//		if(!NumberUtil.isPhoneNumber(tel)   && !tel.startsWith("666")){		 
//			return PackageUtil.packageParams(10701,"手机号码格式错误","","");
//		}
		MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
		if(vo==null){
			return PackageUtil.packageParams(10704,PropertieUtil.loadLangPropertie(lang, 10704+""),"",""); 
		}
		/*if(vo.getPwd() == null || "".equals(vo.getPwd())){
			return PackageUtil.packageParams(10715,"尚未设置密码","",""); 
		}*/
		//验证密码
		if(memberVO.getPwd()==null || memberVO.getPwd().length() != 32){
			return PackageUtil.packageParams(10712,PropertieUtil.loadLangPropertie(lang, 10712+""),"",""); 
		}
		
		//登录
		memberVO = memberService.login(memberVO);
		if(memberVO == null){
			return PackageUtil.packageParams(10705,PropertieUtil.loadLangPropertie(lang, 10705+""),"","");
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,PropertieUtil.loadLangPropertie(lang, 10710+""),"",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getId());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			//注册推送
			memberService.regPushId(memberVO.getId(), memberVO.getId());
			//登录信息
			memberVO = memberService.getLoginMemberById(memberVO.getId());
			return PackageUtil.packageParams(200,"success",memberVO,""); 
		}
	}
	

	/**
	 *  根据手机号码
	 *  获取 1登录 2注册 3找回密码 type
	 *   短信 验证码
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/sendSms",method=RequestMethod.GET)
	public Object loginSms(@RequestHeader (value="LANG", required=false) String lang,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO,   String type) throws Exception{
		//类型
		if(type == null){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		if(!"1".equals(type) && !"2".equals(type)&&!"3".equals(type)  ){
			return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"",""); 
		}
		//验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		
		//验证手机号码
		String tel = memberVO.getTel();
		if(tel == null || "".equals(tel.trim())){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		if("+86".equals(memberVO.getCountry()) && tel.length() != 11){
			return PackageUtil.packageParams(10701,PropertieUtil.loadLangPropertie(lang, 10701+""),"",""); 
		}
//		if(!NumberUtil.isPhoneNumber(tel) && !tel.startsWith("666")){			 
//			return PackageUtil.packageParams(10701,"手机号码格式错误","",""); 
//		}
		MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
		//登录 找回
		if("3".equals(type)){
			if(vo==null){
				return PackageUtil.packageParams(10704,PropertieUtil.loadLangPropertie(lang, 10704+""),"",""); 
			}
		}else if("2".equals(type)){
			if(vo!=null){
				return PackageUtil.packageParams(10702,PropertieUtil.loadLangPropertie(lang, 10702+""),"",""); 
			}
		}
		//验证短信相关
		String ip = IPUtil.getIP(request);
		int todaySms = smsService.checkTodayNum(ip,Integer.parseInt(type),memberVO.getCountry_code()+tel);
		if(todaySms > 10){
			return PackageUtil.packageParams(1011,PropertieUtil.loadLangPropertie(lang, 1011+""),"",""); 
		}
		int num = smsService.checkSmsNoTime(ip,Integer.parseInt(type), memberVO.getCountry_code()+tel);
		if(num==1){
			return PackageUtil.packageParams(1016,PropertieUtil.loadLangPropertie(lang, 1016+""),"",""); 
		}
		int hourNum = smsService.checkHourNum(ip,Integer.parseInt(type), memberVO.getCountry_code()+tel);
		if(hourNum > 5){
			return PackageUtil.packageParams(1018,PropertieUtil.loadLangPropertie(lang, 1018+""),"",""); 
		}
		
		if(tel.startsWith("666")){
			Map map = new HashMap();
			map.put("sms_no", "123456");
			//记录验证码
			smsService.insertSmsLog(ip,"123456",memberVO.getCountry_code()+tel,Integer.parseInt(type));
			return PackageUtil.packageParams(200,"success",map,""); 
		}else{
			//调用短信接口
			//验证码发送失败返回 判断
			String sms_no = SmsSendUtil.sendSms(tel,memberVO.getCountry_code());
			if(sms_no != null){
				//记录验证码
				smsService.insertSmsLog(ip,sms_no,memberVO.getCountry_code()+tel,Integer.parseInt(type));
				return PackageUtil.packageParams(200,"success","",""); 
			}else{
				return PackageUtil.packageParams(1015,PropertieUtil.loadLangPropertie(lang, 1015+""),"",""); 
			}
		}
	}
	/**
	 * 短信登录
	 * @param memberVO 
	 * tel 手机号码
	 * sms_no 验证码
	 * @return 
	 * cuid 会员id
	 * token 令牌
	 */
	@ResponseBody
	@RequestMapping(value="/loginSmsSub",method=RequestMethod.GET)
	public Object loginSmsSub(@RequestHeader (value="LANG", required=false) String lang,@RequestHeader (value="OS", required=false) String os,HttpServletRequest request, HttpServletResponse response
			, MemberVO memberVO, String open_id, String sina_id, String qq_id, String push_open_id, String vkontakte_id, String facebook_id, String google_id){
		// 验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		// 验证手机号码
		String tel = memberVO.getTel();
		if (tel == null || "".equals(tel.trim())) {
			return PackageUtil.packageParams(10700, "手机号码不存在", "", "");
		}
//		if (!NumberUtil.isPhoneNumber(tel) && !tel.startsWith("666")) { 
//			return PackageUtil.packageParams(10701, "手机号码格式错误", "", "");
//		}
		//验证码检查
		if(memberVO.getSms_no()==null){
			return PackageUtil.packageParams(1012,"请输入验证码","",""); 
		}
		if(memberVO.getSms_no().length() != 6 ){
			return PackageUtil.packageParams(1013,"验证码格式错误","",""); 
		}
		memberVO.setIp(IPUtil.getIP(request));
		memberVO.setType("1");
		int exitNum = smsService.checkSmsExit(memberVO);
		if(exitNum == 0){
			return PackageUtil.packageParams(1013,"验证码错误","",""); 
		}
		//验证码超时
		int num = smsService.checkSmsNoOutTime(memberVO);
		if(num > 0){
			return PackageUtil.packageParams(1014,"验证码失效，请重新获取验证码","",""); 
		}
	
		//手机号是否注册 放在检查验证码之后 登录手机号不存在自动
		MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
		if (vo == null) {
			return PackageUtil.packageParams(10704, "该手机号未注册", "", "");
		}
		//短信登录
		memberVO = memberService.smsLogin(memberVO);
		if(memberVO == null){
			return PackageUtil.packageParams(1013,"验证码错误","",""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//绑定OPEN_ID
			if(open_id != null){
				//检查open_id是否已绑定
				MemberVO bindVO = memberService.getAllInfoById(memberVO.getId());
				if(bindVO.getOpen_id() != null){
					return PackageUtil.packageParams(10716,"该手机号已绑定其他微信号","","该手机号已绑定其他微信号");
				}
				MemberVO open_member = memberService.getLoginMemberByOpenId(open_id);
				if(open_member == null){
					memberVO.setOpen_id(open_id);
					memberService.updateOpenId(memberVO);
				}else{
					return PackageUtil.packageParams(10714,"该微信号已注册","","");
				}
			}
			//绑定新浪ID
			if(sina_id != null){
				//检查open_id是否已绑定
				MemberVO bindVO = memberService.getAllInfoById(memberVO.getId());
				if(bindVO.getSina_id() != null){
					return PackageUtil.packageParams(10716,"该手机号已绑定其他新浪号","","该手机号已绑定其他新浪号");
				}
				MemberVO sina_member = memberService.getLoginMemberBySinaId(sina_id);
				if(sina_member == null){
					memberVO.setSina_id(sina_id);
					memberService.updateSinaId(memberVO);
				}else{
					return PackageUtil.packageParams(10714,"该新浪号已注册","","");
				}
			}
			//绑定QQ ID 
			if(qq_id != null){
				//检查open_id是否已绑定
				MemberVO bindVO = memberService.getAllInfoById(memberVO.getId());
				if(bindVO.getQq_id() != null){
					return PackageUtil.packageParams(10716,"该手机号已绑定其他QQ号","","该手机号已绑定其他QQ号");
				}
				MemberVO qq_member = memberService.getLoginMemberByQQId(qq_id);
				if(qq_member == null){
					memberVO.setQq_id(qq_id);
					memberService.updateQQId(memberVO);
				}else{
					return PackageUtil.packageParams(10714,"该QQ号已注册","","");
				}
			}
			//绑定google_id 
			if(google_id != null){
				//检查google_id是否已绑定
				MemberVO bindVO = memberService.getAllInfoById(memberVO.getId());
				if(bindVO.getGoogle_id() != null){
					return PackageUtil.packageParams(10716,"该手机号已绑定其他google号","","该手机号已绑定其他google号");
				}
				MemberVO google_member = memberService.getLoginMemberByGoogleId(google_id);
				if(google_member == null){
					memberVO.setGoogle_id(google_id);
					memberService.updateGoogleId(memberVO);
				}else{
					return PackageUtil.packageParams(10714,"该google号已注册","","");
				}
			}
			//绑定facebook_id
			if(facebook_id != null){
				//检查open_id是否已绑定
				MemberVO bindVO = memberService.getAllInfoById(memberVO.getId());
				if(bindVO.getFacebook_id() != null){
					return PackageUtil.packageParams(10716,"该手机号已绑定其他facebook_id号","","该手机号已绑定其他facebook_id号");
				}
				MemberVO facebook_member = memberService.getLoginMemberByFacebookId(facebook_id);
				if(facebook_member == null){
					memberVO.setFacebook_id(facebook_id);
					memberService.updateFacebookId(memberVO);
				}else{
					return PackageUtil.packageParams(10714,"该facebook_id号已注册","","");
				}
			}
			//绑定vkontakte_id
			if(vkontakte_id != null){
				//检查open_id是否已绑定
				MemberVO bindVO = memberService.getAllInfoById(memberVO.getId());
				if(bindVO.getVkontakte_id() != null){
					return PackageUtil.packageParams(10716,"该手机号已绑定其他vkontakte_id号","","该手机号已绑定其他vkontakte_id号");
				}
				MemberVO vkontakte_member = memberService.getLoginMemberByVkontakteId(vkontakte_id);
				if(vkontakte_member == null){
					memberVO.setVkontakte_id(vkontakte_id);
					memberService.updateVkontakteId(memberVO);
				}else{
					return PackageUtil.packageParams(10714,"该Vkontakte号已注册","","");
				}
			}
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getId());
			map.put("token", MD5Util.MD5Encode(StringUtil.getRandomString(16), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			//注册推送
			memberService.regPushId(memberVO.getId(), memberVO.getId());
			//登录信息
			memberVO = memberService.getLoginMemberById(memberVO.getId());
			if(memberVO.getPush_open_id() == null && push_open_id != null && !"".equals(push_open_id)){
				memberService.updatePushOpenId(memberVO.getId(), push_open_id);
				memberVO.setPush_open_id(push_open_id);
			}
			return PackageUtil.packageParams(200,"success",memberVO,""); 
		}
	}
	
	/**
	 * 验证注册验证码
	 * 验证手机号是否存在
	 */
	@ResponseBody
	@RequestMapping(value="/checkRegSms",method=RequestMethod.GET)
	public Object checkRegSms(@RequestHeader (value="LANG", required=false) String lang, @RequestHeader (value="OS", required=false) String os, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		memberVO.setType("2");
		//验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//验证手机号码
		String tel = memberVO.getTel();
		if(tel == null || "".equals(tel.trim())){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
		if(vo!=null){
			return PackageUtil.packageParams(10702,PropertieUtil.loadLangPropertie(lang, 10702+""),"",""); 
		}
		//验证码检查
		if (memberVO.getSms_no() == null) {
			return PackageUtil.packageParams(1012,PropertieUtil.loadLangPropertie(lang, 1012+""),"",""); 
		}
		if (memberVO.getSms_no().length() != 6) {
			return PackageUtil.packageParams(1013,PropertieUtil.loadLangPropertie(lang, 1013+""),"",""); 
		}
		// 验证码超时
		memberVO.setIp(IPUtil.getIP(request));
		int num = smsService.checkSmsNoOutTime(memberVO);
		if (num > 0) {
			return PackageUtil.packageParams(1014,PropertieUtil.loadLangPropertie(lang, 1014+""),"",""); 
		}
		//查询验证码是否正确
		int re = smsService.checkSmsReg(memberVO);
		if(re==0){
			return PackageUtil.packageParams(1013,PropertieUtil.loadLangPropertie(lang, 1013+""),"",""); 
		}else{
			return PackageUtil.packageParams(200,"success","",""); 
		}
	}
	
	/**
	 *  注册提交
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/regSub",method=RequestMethod.POST)
	public Object regSub(@RequestHeader (value="LANG", required=false) String lang, @RequestHeader (value="OS", required=false) String os,  HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		//验证type  1登录时注册  2注册
		if(memberVO.getType()==null || "".equals(memberVO.getType())){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		if(!"1".equals(memberVO.getType()) && !"2".equals(memberVO.getType()) ){
			return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"",""); 
		}
		//验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//验证手机号码
		String tel = memberVO.getTel();
		if(tel == null || "".equals(tel.trim())){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
//		if(!NumberUtil.isPhoneNumber(tel) && !tel.startsWith("666")){
//			return PackageUtil.packageParams(10701,"手机号码格式错误","",""); 
//		}
		MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
		if(vo!=null){
			return PackageUtil.packageParams(10702,PropertieUtil.loadLangPropertie(lang, 10702+""),"",""); 
		}
		//验证码检查
		if (memberVO.getSms_no() == null) {
			return PackageUtil.packageParams(1012,PropertieUtil.loadLangPropertie(lang, 1012+""),"",""); 
		}
		if (memberVO.getSms_no().length() != 6) {
			return PackageUtil.packageParams(1013,PropertieUtil.loadLangPropertie(lang, 1013+""),"",""); 
		}
		// 验证码超时
		memberVO.setIp(IPUtil.getIP(request));
		int num = smsService.checkSmsNoOutTime(memberVO);
		if (num > 0) {
			return PackageUtil.packageParams(1014,PropertieUtil.loadLangPropertie(lang, 1014+""),"",""); 
		}
		//邀请登录   //"null"兼容微信端
		if(memberVO.getInvite_no() != null && !"null".equals(memberVO.getInvite_no())){
			//判断邀请人存在
			int reNum = memberService.checkInviteNo(memberVO.getInvite_no());
			if(reNum==0){
				return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"",""); 
			}
			if("pe".equals(WebContextListener.env_code)){
				//如果是666的只能绑定666的
				MemberVO m = memberService.getMemberInfoById(memberVO.getInvite_no());
				if(memberVO.getTel().startsWith("666") && !m.getTel().startsWith("666")){
					return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"","不可绑定非测试帐号"); 
				}
				if(!memberVO.getTel().startsWith("666") && m.getTel().startsWith("666")){
					return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"","不可绑定非测试帐号"); 
				}
			}
			/*String ranPwd = StringUtil.getRandomNumberString(6);
			memberVO.setPwd(MD5Util.MD5Encode(ranPwd, "utf-8"));
			SmsSendUtil.sendSmsPwd(tel, memberVO.getCountry(), ranPwd);*/
			
			//送等级验证
			if(memberVO.getExp_level() != null){
				//体验期内不能送
				MemberVO inveteMember = memberService.getAllByInviteNo(memberVO.getInvite_no());
				if("1".equals(inveteMember.getExp_state())){
					return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"",""); 
				}
				//等级不对不能送
				if(!PropertieUtil.loadPropertie("primary_level").equals(memberVO.getExp_level())){
					return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"",""); 
				}
				int expNum = memberLevelItemService.compareLevel(inveteMember.getLevel(), memberVO.getExp_level()); 
				if(expNum < 0){
					return PackageUtil.packageParams(10000,PropertieUtil.loadLangPropertie(lang, 10000+""),"",""); 
				}
			}
		}
		//验证密码
		if(memberVO.getPwd()!=null && memberVO.getPwd().length() != 32){
			return PackageUtil.packageParams(10712,PropertieUtil.loadLangPropertie(lang, 10712+""),"",""); 
		}
		//查询验证码是否正确
		int re = smsService.checkSmsReg(memberVO);
		if(re==0){
			return PackageUtil.packageParams(1013,PropertieUtil.loadLangPropertie(lang, 1013+""),"",""); 
		}else{
			//注册提交
			memberVO.setLevel(PropertieUtil.loadPropertie("common_level"));
			//微信注册
			if(memberVO.getOpen_id() != null && !"".equals(memberVO.getOpen_id())){
				//检查open_id是否已绑定
				MemberVO open_member = memberService.getLoginMemberByOpenId(memberVO.getOpen_id());
				if(open_member != null){
					return PackageUtil.packageParams(10714,"该微信号已注册","","");
				}
				//绑定预绑定推荐人
//				String invite_no = memberService.getInvitenoByOpenid(memberVO.getOpen_id());
//				if(invite_no !=null){
//					memberVO.setInvite_no(invite_no);
//				}
			}
			if(memberVO.getSina_id() != null && !"".equals(memberVO.getSina_id())){
				MemberVO sina_member = memberService.getLoginMemberBySinaId(memberVO.getSina_id());
				if(sina_member != null){
					return PackageUtil.packageParams(10714,"该新浪号已注册","","");
				}
			}
			if(memberVO.getQq_id() != null && !"".equals(memberVO.getQq_id())){
				MemberVO qq_member = memberService.getLoginMemberByQQId(memberVO.getQq_id());
				if(qq_member != null){
					return PackageUtil.packageParams(10714,"该QQ号已注册","","");
				}
			}
			if(memberVO.getGoogle_id() != null && !"".equals(memberVO.getGoogle_id())){
				MemberVO google_member = memberService.getLoginMemberByGoogleId(memberVO.getGoogle_id());
				if(google_member != null){
					return PackageUtil.packageParams(10714,"该谷歌号已注册","","");
				}
			}
			if(memberVO.getFacebook_id() != null && !"".equals(memberVO.getFacebook_id())){
				MemberVO facebook_member = memberService.getLoginMemberByFacebookId(memberVO.getFacebook_id());
				if(facebook_member != null){
					return PackageUtil.packageParams(10714,"该facebook号已注册","","");
				}
			}
			if(memberVO.getVkontakte_id() != null && !"".equals(memberVO.getVkontakte_id())){
				MemberVO vkontakte_member = memberService.getLoginMemberByVkontakteId(memberVO.getVkontakte_id());
				if(vkontakte_member != null){
					return PackageUtil.packageParams(10714,"该vkontakte已注册","","");
				}
			}
			int insertNum = memberService.insert(memberVO);
			if(insertNum > 0){
				int regNum = memberService.getMemberCountByTelAndCountry(memberVO);
				if(regNum > 1){
					memberService.deleteMember(memberVO.getId());
					memberVO = memberService.getMemberByTelAndCountry(memberVO);
				}
				//生成关联表数据
				memberService.insertConnTable(memberVO);
				//成功邀请推送
				pushService.pushInvitedByMsg(memberVO);
				
				//登录成功
				Map<String, String> map = new HashMap<String, String>();
				map.put("cuid", memberVO.getId());
				map.put("token", MD5Util.MD5Encode(StringUtil.getRandomString(16), "utf-8"));
				map.put("os", os);
				//更新token和最后登录时间
				memberService.updateTokenAndLogin(map);
				//注册推送
				memberService.regPushId(memberVO.getId(), memberVO.getId());
				//登录信息
				MemberVO newMember = memberService.getLoginMemberById(memberVO.getId());
				//修改赠送等级会员消息
				//添加赠送log
				if("1".equals(newMember.getExp_state())){
					memberService.updateExpMsg(newMember.getId());
					memberService.insertLogExp(newMember.getId(),newMember.getLevel(),newMember.getInvited_by_id());
				}
				return PackageUtil.packageParams(200,"success",newMember,""); 
			}
		}
		return PackageUtil.packageParams(10708,"failed",null,""); 
	}
	
	/**
	 *  修改密码
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/pwdUpdateSub",method=RequestMethod.POST)
	public Object pwdUpdateSub(@RequestHeader (value="LANG", required=false) String lang, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		//验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//验证手机号码
		String tel = memberVO.getTel();
		if(tel == null || "".equals(tel.trim())){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
//		if(!NumberUtil.isPhoneNumber(tel) && !tel.startsWith("666")){
//			return PackageUtil.packageParams(10701,"手机号码格式错误","",""); 
//		}
		MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
		if(vo==null){
			return PackageUtil.packageParams(10704,PropertieUtil.loadLangPropertie(lang, 10704+""),"",""); 
		}
		//验证码检查
		if (memberVO.getSms_no() == null || "".equals(memberVO.getSms_no())) {
			return PackageUtil.packageParams(1012,PropertieUtil.loadLangPropertie(lang, 1012+""),"",""); 
		}
		if (memberVO.getSms_no().length() != 6) {
			return PackageUtil.packageParams(1013,PropertieUtil.loadLangPropertie(lang, 1013+""),"",""); 
		}
		// 验证码超时
		memberVO.setIp(IPUtil.getIP(request));
		if(memberVO.getType() == null){
			memberVO.setType("3");
		}
		int num = smsService.checkSmsNoOutTime(memberVO);
		if (num > 0) {
			return PackageUtil.packageParams(1014,PropertieUtil.loadLangPropertie(lang, 1014+""),"",""); 
		}
		//验证密码
		if(memberVO.getPwd()==null || memberVO.getPwd().length() != 32){
			return PackageUtil.packageParams(10712,PropertieUtil.loadLangPropertie(lang, 10712+""),"",""); 
		}
		//查询验证码是否正确
		int re = smsService.checkSmsReg(memberVO);
		if(re==0){
			return PackageUtil.packageParams(1013,PropertieUtil.loadLangPropertie(lang, 1013+""),"",""); 
		}else{
			//修改密码提交
			memberService.updatePwd(memberVO);
			return PackageUtil.packageParams(200,"success","",""); 
		}
	}
	
	/**
	 * 验证并更新token 
	 * APP
	 */
	@ResponseBody
	@RequestMapping(value="/updateToken",method=RequestMethod.POST)
	public Object updateToken(@RequestHeader (value="LANG", required=false) String lang, @RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,
			HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		//查询并更新TOKEN时间
		//检查上次登录是否在七天内 过期则需重新登录
		int num = memberService.checkTokenTime(token,member_id); 
		//一周内未登录过 返回错误码
		if(num==0){
			return PackageUtil.packageParams(10712,PropertieUtil.loadLangPropertie(lang, 10712+""),"",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", member_id);
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			//注册推送
			memberService.regPushId(memberVO.getId(), memberVO.getId());
			//登录信息
			memberVO = memberService.getLoginMemberById(member_id);
			return PackageUtil.packageParams(200,"success",memberVO,""); 
		}
	}
	
	/**
	 * 根据sina_id登录 
	 * @param sina_id
	 */
	@ResponseBody
	@RequestMapping(value="/sinaLogin",method=RequestMethod.GET)
	public Object sinaLogin(@RequestHeader (value="OS", required=false) String os,String sina_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		if(sina_id == null || "".equals(sina_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		// 登录信息
		memberVO = memberService.getLoginMemberBySinaId(sina_id);
		if(memberVO==null){
			return PackageUtil.packageParams(10704,"该用户未注册",new MemberVO(),""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getCuid());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			memberVO = memberService.getLoginMemberById(memberVO.getCuid());
			return PackageUtil.packageParams(200, "success", memberVO, "");
		}
	}
	
	/**
	 * 根据qqid登录 qq
	 * @param qq_id
	 */
	@ResponseBody
	@RequestMapping(value="/qqLogin",method=RequestMethod.GET)
	public Object qqLogin(@RequestHeader (value="OS", required=false) String os,String qq_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		if(qq_id == null || "".equals(qq_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		// 登录信息
		memberVO = memberService.getLoginMemberByQQId(qq_id);
		if(memberVO==null){
			return PackageUtil.packageParams(10704,"该用户未注册",new MemberVO(),""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getCuid());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			memberVO = memberService.getLoginMemberById(memberVO.getCuid());
			return PackageUtil.packageParams(200, "success", memberVO, "");
		}
	}
	
	/**
	 * 根据openid登录 微信
	 * @param open_id
	 */
	@ResponseBody
	@RequestMapping(value="/wechatLogin",method=RequestMethod.GET)
	public Object wechatLogin(@RequestHeader (value="OS", required=false) String os, String open_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO, String push_open_id) throws Exception{
		if(open_id == null || "".equals(open_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		// 登录信息
		memberVO = memberService.getLoginMemberByOpenId(open_id);
		if(memberVO==null){
			return PackageUtil.packageParams(10704,"该用户未注册",new MemberVO(),""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getCuid());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			if(os==null)os="wechat";//兼容旧版微信的bug 新版可以删除
			map.put("os", os);//
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			memberVO = memberService.getLoginMemberById(memberVO.getCuid());
			if(push_open_id != null && memberVO.getPush_open_id() == null){
				memberService.updatePushOpenId(memberVO.getId(), push_open_id);
			}
			return PackageUtil.packageParams(200, "success", memberVO, "");
		}
	}
	
	/**
	 * 根据google登录 google
	 * @param google
	 */
	@ResponseBody
	@RequestMapping(value="/googleLogin",method=RequestMethod.GET)
	public Object googleLogin(@RequestHeader (value="OS", required=false) String os,String google_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		if(google_id == null || "".equals(google_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		// 登录信息
		memberVO = memberService.getLoginMemberByGoogleId(google_id);
		if(memberVO==null){
			return PackageUtil.packageParams(10704,"该用户未注册",new MemberVO(),""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getCuid());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			memberVO = memberService.getLoginMemberById(memberVO.getCuid());
			return PackageUtil.packageParams(200, "success", memberVO, "");
		}
	}
	/**
	 * 根据google登录 google
	 * @param google
	 */
	@ResponseBody
	@RequestMapping(value="/facebookLogin",method=RequestMethod.GET)
	public Object facebookLogin(@RequestHeader (value="OS", required=false) String os,String facebook_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		if(facebook_id == null || "".equals(facebook_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		// 登录信息
		memberVO = memberService.getLoginMemberByFacebookId(facebook_id);
		if(memberVO==null){
			return PackageUtil.packageParams(10704,"该用户未注册",new MemberVO(),""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getCuid());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			memberVO = memberService.getLoginMemberById(memberVO.getCuid());
			return PackageUtil.packageParams(200, "success", memberVO, "");
		}
	}
	/**
	 * 根据google登录 google
	 * @param google
	 */
	@ResponseBody
	@RequestMapping(value="/vkontakteLogin",method=RequestMethod.GET)
	public Object vkontakteLogin(@RequestHeader (value="OS", required=false) String os,String vkontakte_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		if(vkontakte_id == null || "".equals(vkontakte_id)){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
		// 登录信息
		memberVO = memberService.getLoginMemberByVkontakteId(vkontakte_id);
		if(memberVO==null){
			return PackageUtil.packageParams(10704,"该用户未注册",new MemberVO(),""); 
		}else if("1".equals(memberVO.getState())){
			return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
		}else{
			//登录成功
			Map<String, String> map = new HashMap<String, String>();
			map.put("cuid", memberVO.getCuid());
			map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
			map.put("os", os);
			//更新token和最后登录时间
			memberService.updateTokenAndLogin(map);
			memberVO = memberService.getLoginMemberById(memberVO.getCuid());
			return PackageUtil.packageParams(200, "success", memberVO, "");
		}
	}
	
	/**
	 *  微信已注册登录
	 *  未绑定open_id
	 * @throws Exception 
	 */
	@ResponseBody
	@RequestMapping(value="/wechatLoginSub",method=RequestMethod.POST)
	public Object wechatLoginSub(@RequestHeader (value="LANG", required=false) String lang, String open_id,HttpServletRequest request, HttpServletResponse response, MemberVO memberVO, String push_open_id) throws Exception{
		//验证国家
		if(memberVO.getCountry()==null && memberVO.getCountry_id()==null ){
			return PackageUtil.packageParams(10001,PropertieUtil.loadLangPropertie(lang, 10001+""),"",""); 
		}
		//旧版本
		if(memberVO.getCountry()!=null){
			CityVO cityVO = cityService.getCountryByAreaCode(memberVO.getCountry());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//新版本
		if(memberVO.getCountry_id() != null){
			CityVO cityVO = cityService.getCityById(memberVO.getCountry_id());
			if(cityVO == null){
				return PackageUtil.packageParams(1017,PropertieUtil.loadLangPropertie(lang, 1017+""),"",""); 
			}else{
				memberVO.setCountry_id(cityVO.getId());
				memberVO.setCountry_code(cityVO.getArea_code());
			}
		}
		//验证手机号码
		String tel = memberVO.getTel();
		if(tel == null || "".equals(tel.trim())){
			return PackageUtil.packageParams(10001,"缺少必选参数","",""); 
		}
//		if(!NumberUtil.isPhoneNumber(tel) && !tel.startsWith("666")){
//			return PackageUtil.packageParams(10701,"手机号码格式错误","",""); 
//		}
		//验证码检查
		if (memberVO.getSms_no() == null) {
			return PackageUtil.packageParams(1012, "请输入验证码", "", "");
		}
		if (memberVO.getSms_no().length() != 6) {
			return PackageUtil.packageParams(1013, "验证码格式错误", "", "");
		}
		// 验证码超时
		memberVO.setIp(IPUtil.getIP(request));
		int num = smsService.checkSmsNoOutTime(memberVO);
		if (num > 0) {
			return PackageUtil.packageParams(1014, "验证码失效，请重新获取验证码", "", "");
		}
		//查询验证码是否正确
		int re = smsService.checkSmsReg(memberVO);
		if(re==0){
			return PackageUtil.packageParams(1013,"验证码错误","",""); 
		}else{
			MemberVO vo = memberService.getMemberByTelAndCountry(memberVO);
			if(vo!=null){
				if(vo.getOpen_id()!=null  && !"".equals(vo.getOpen_id()) ){
					return PackageUtil.packageParams(10713,"该手机号已绑定公众号","",""); 
				}else if("1".equals(vo.getState())){
					return PackageUtil.packageParams(10710,"帐号已锁定","",""); 
				}else{
					//绑定OPEN_ID
					if(open_id != null && !"".equals(open_id)){
						//检查open_id是否已绑定
						MemberVO bindVO = memberService.getAllInfoById(vo.getId());
						if(bindVO.getOpen_id() != null){
							return PackageUtil.packageParams(10716,"该手机号已绑定其他微信号","","该手机号已绑定其他微信号");
						}
						MemberVO open_member = memberService.getLoginMemberByOpenId(open_id);
						if(open_member == null){
							vo.setOpen_id(open_id);
							vo.setPush_open_id(push_open_id);
							memberService.updateOpenId(vo);
						}else{
							return PackageUtil.packageParams(10714,"该微信号已注册","","");
						}
					}
					//登录成功
					Map<String, String> map = new HashMap<String, String>();
					map.put("cuid", vo.getId());
					map.put("token", MD5Util.MD5Encode(PayCommonUtil.CreateNoncestr(), "utf-8"));
					map.put("os", "wechat");
					//更新token和最后登录时间
					memberService.updateTokenAndLogin(map);
					memberVO = memberService.getLoginMemberById(vo.getId());
					return PackageUtil.packageParams(200,"success",memberVO,"");
				}
			}else{
				return PackageUtil.packageParams(10704,"该用户未注册","",""); 
			}
		}
	}
	
	
	/**
	 * 解绑
	 * @param open_id
	 */
	@ResponseBody
	@RequestMapping(value="/wechatLoginOut",method=RequestMethod.GET)
	public Object wechatLoginOut(@RequestHeader (value="OS", required=false) String os,@RequestHeader (value="AUTHORIZATION", required=false) String token,@RequestHeader (value="SECRET", required=false)  String member_id,String open_id, HttpServletRequest request, HttpServletResponse response, MemberVO memberVO) throws Exception{
		if("wechat".equals(os)){
			memberVO = memberService.getMemberByIdWechat(member_id, token);
			if(memberVO!=null){
				memberVO.setOpen_id(null);
				memberVO.setPush_open_id(null);
				memberService.updateOpenId(memberVO);
			}
			return PackageUtil.packageParams(200,"success",memberVO,""); 
		}else{
			return PackageUtil.packageParams(403,"禁止访问","",""); 
		}
	}
}
